package com.example.product.controller;

import com.example.homework.Show;
import com.example.product.bean.Product;
import com.example.product.service.ProductService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

////网关路径
//@RequestMapping("/api/product")

@RestController
@Slf4j
public class ProductController {

    //拿到配置文件中的服务名和端口号
    @Value("${spring.application.name}")
    String serviceName;
    @Value("${server.port}")
    int port;

    @Autowired
    ProductService productService;

    //查询商品
    @GetMapping("/product/{id}")

    //c尝试接收拦截器更改后的请求头
    public Product queryProduct(@PathVariable("id") Long productId,
                                HttpServletRequest request) {

        String header =  request.getHeader("X-Request-ID");
        System.out.println("拦截器更改的请求头是："+header);

        Product product = productService.getProductById(productId);
        System.out.println("对的");
        return product;
    }


    @GetMapping("/show/{id}")
    public Show showProduct(@PathVariable("id") Long id
    , HttpServletRequest request) {
        Show show = new Show();
        show.setServiceName("远程调用的服务名为："+serviceName+"，端口号为："+port+"传参为："+id);
        show.setPort(port);

        log.info("服务端收到请求");
        return show;
    }




}
